package br.com.formulavendas.businessusercase;

// default package

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.formulavendas.domainmodel.Cargo;


/**
 * @see .Cargo
 * @author MyEclipse Persistence Tools
 */
public class ManterCargo extends BaseBusinessUserCase
	implements br.com.formulavendas.businessusercase.baseinterface.IBasePersistence<Cargo>,
				br.com.formulavendas.businessusercase.baseinterface.IBaseFind<Cargo> {

	public ManterCargo() { }

	private static final Logger log = LoggerFactory
			.getLogger(ManterCargo.class);
	
	// property constants
	public static final String NOME = "nome";

	@Override
	public void save(Cargo transientInstance) {
		Transaction tx = this._session.beginTransaction();
		log.debug("saving Cargo instance");
		try {
			this._session.save(transientInstance);
			tx.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			tx.rollback();
			log.error("save failed", re);
			throw re;
		}
	}

	@Override
	public void delete(Cargo persistentInstance) {
		log.debug("deleting Cargo instance");
		try {
			this._session.delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	@Override
	public Cargo findById(java.lang.Integer id) {
		log.debug("getting Cargo instance with id: " + id);
		try {
			Cargo instance = (Cargo) this._session.get("Cargo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Cargo> findByExample(Cargo instance) {
		log.debug("finding Cargo instance by example");
		try {
			List results = this._session.createCriteria("Cargo")
					.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	@Override
	public List<Cargo> findByProperty(String propertyName, Object value) {
		log.debug("finding Cargo instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Cargo as model where model."
					+ propertyName + "= ?";
			Query queryObject = this._session.createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Cargo> findByNome(Object nome) {
		return findByProperty(NOME, nome);
	}

	@Override
	public List<Cargo> findAll() {
		log.debug("finding all Cargo instances");
		try {
			String queryString = "from Cargo";
			Query queryObject = this._session.createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Cargo merge(Cargo detachedInstance) {
		log.debug("merging Cargo instance");
		try {
			Cargo result = (Cargo) this._session.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Cargo instance) {
		log.debug("attaching dirty Cargo instance");
		try {
			this._session.saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Cargo instance) {
		log.debug("attaching clean Cargo instance");
		try {
			this._session.lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}